//
//  MbFlash.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbFlashAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import "GDTSplashAd.h"
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>

@interface MbFlashAd()<BUSplashAdDelegate,GDTSplashAdDelegate,KSSplashAdViewDelegate,ABUSplashAdDelegate>

@property(nonatomic,strong) BUSplashAdView *ttSplashView;

@property(nonatomic,strong) GDTSplashAd *gdtSplashAd;
@property(nonatomic,strong) KSSplashAdView *ksSplashAdView;
@property(nonatomic,strong) ABUSplashAd *gromoreSplashAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end



@implementation MbFlashAd



-(void)loadFlash{
    if(!self.adItems || self.adItems.count == self.index || !self.rootVC){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(splashAdDidError:)]) {
            [self.delegate splashAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        CGRect frame = [UIScreen mainScreen].bounds;
        BUAdSlot *buadSlot = [BUAdSlot new];
        buadSlot.ID = adItem.codeNo;
        buadSlot.splashButtonType = 1;
        self.ttSplashView = [[BUSplashAdView alloc] initWithSlot:buadSlot frame:frame];
        // tolerateTimeout = CGFLOAT_MAX , The conversion time to milliseconds will be equal to 0
        self.ttSplashView .tolerateTimeout = 3;
        self.ttSplashView .delegate = self;
        [self.ttSplashView  loadAdData];
        [self.rootVC.view addSubview: self.ttSplashView ];
        self.ttSplashView .rootViewController = self.rootVC;
    } else if(adItem.plat == GDT){
        
        self.gdtSplashAd =  [[GDTSplashAd alloc] initWithPlacementId:adItem.codeNo];
        self.gdtSplashAd.delegate = self; //设置代理
        if ([[UIScreen mainScreen] bounds].size.height >= 568.0f) {
            self.gdtSplashAd.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h"]];
        } else {
            self.gdtSplashAd.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage"]];
        }
        self.gdtSplashAd.fetchDelay = 3; //开发者可以设置开屏拉取时间，超时则放弃展示
        [self.gdtSplashAd loadFullScreenAd];
    } else if(adItem.plat == KS){
        self.ksSplashAdView = [[KSSplashAdView alloc] initWithPosId:adItem.codeNo];
        self.ksSplashAdView.delegate = self;
        self.ksSplashAdView.rootViewController = self.rootVC;
        [self.ksSplashAdView loadAdData];
        [self.rootVC.view addSubview:self.ksSplashAdView];
    } else if(adItem.plat == GroMore){
        self.gromoreSplashAd = [[ABUSplashAd alloc] initWithAdUnitID:adItem.codeNo];
        self.gromoreSplashAd.rootViewController = self.rootVC;    //required
        self.gromoreSplashAd.delegate = self;
        self.gromoreSplashAd.tolerateTimeout = 3.f;
        [self.gromoreSplashAd loadAdData];
    }
    self.index ++;
    
    
}


/// 加载成功
/// @param splashAd <#splashAd description#>
-(void)splashAdDidLoad:(NSObject *)splashAd{
    NSLog(@"开屏加载成功");
    if([splashAd isKindOfClass:[GDTSplashAd class]]){
        [self.gdtSplashAd showFullScreenAdInWindow:[UIApplication sharedApplication].keyWindow withLogoImage:nil skipView:nil];
    }
    if([splashAd isKindOfClass:[ABUSplashAd class]]){
        [self.gromoreSplashAd showInWindow:[UIApplication sharedApplication].keyWindow];
    }
    
    if ([self.delegate respondsToSelector:@selector(splashAdDidLoad)]) {
        [self.delegate splashAdDidLoad];
    }
}


/// 此回调在广告跳转到其他控制器时，该控制器被关闭时调用。interactionType：此参数可区分是打开的appstore/网页/视频广告详情页面
/// @param splashAd <#splashAd description#>
/// @param interactionType <#interactionType description#>
- (void)splashAdDidCloseOtherController:(BUSplashAdView *)splashAd interactionType:(BUInteractionType)interactionType{
    NSLog(@"穿山甲开屏其他关闭点击");
    if ([self.delegate respondsToSelector:@selector(splashAdDidClick)]) {
        [self.delegate splashAdDidClose];
    }
}

-(void)splashAdDidClickSkip:(BUSplashAdView *)splashAd{
    if (self.ttSplashView) {
        [self.ttSplashView removeFromSuperview];
        self.ttSplashView = nil;
    }
    NSLog(@"穿山甲开屏跳过");
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}

-(void)splashAdDidClick:(BUSplashAdView *)splashAd{
    NSLog(@"穿山甲开屏点击");
    if ([self.delegate respondsToSelector:@selector(splashAdDidClick)]) {
        [self.delegate splashAdDidClick];
    }
}

/// 倒计时为0时会触发此回调，如果客户端使用了此回调方法，建议在此回调方法中进行广告的移除操作
/// @param splashAd <#splashAd description#>
- (void)splashAdCountdownToZero:(BUSplashAdView *)splashAd{
    if (self.ttSplashView) {
        [self.ttSplashView removeFromSuperview];
        self.ttSplashView = nil;
    }
    NSLog(@"穿山甲开屏倒计时结束");
    
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}


/// 穿山甲失败
/// @param splashAd <#splashAd description#>
/// @param error <#error description#>
- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadFlash];
}

///  优量汇 开屏广告展示失败
/// @param splashAd <#splashAd description#>
/// @param error <#error description#>
- (void)splashAdFailToPresent:(GDTSplashAd *)splashAd withError:(NSError *)error{
    self.error = error;
    [self loadFlash];
}


/// 优量汇广告点击
/// @param splashAd <#splashAd description#>
- (void)splashAdClicked:(GDTSplashAd *)splashAd{
    if ([self.delegate respondsToSelector:@selector(splashAdDidClick)]) {
        [self.delegate splashAdDidClick];
    }
}

///// 开屏广告将要关闭回调
///// @param splashAd <#splashAd description#>
- (void)splashAdDidClose:(ABUSplashAd *)splashAd{
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}

/// 点击以后全屏广告页将要关闭
/// @param splashAd <#splashAd description#>
- (void)splashAdWillDismissFullScreenModal:(NSObject *)splashAd{
    //ABUSplashAd,GDTSplashAd
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}

- (void)ksad_splashAdDidLoad:(KSSplashAdView *)splashAdView{
    if ([self.delegate respondsToSelector:@selector(splashAdDidLoad)]) {
        [self.delegate splashAdDidLoad];
    }
}

- (void)ksad_splashAd:(KSSplashAdView *)splashAdView didSkip:(NSTimeInterval)showDuration{
    if (self.ksSplashAdView) {
        [self.ksSplashAdView removeFromSuperview];
        self.ksSplashAdView = nil;
    }
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}

- (void)ksad_splashAdDidAutoDismiss:(KSSplashAdView *)splashAdView{
    if (self.ksSplashAdView) {
        [self.ksSplashAdView removeFromSuperview];
        self.ksSplashAdView = nil;
    }
    if ([self.delegate respondsToSelector:@selector(splashAdDidClose)]) {
        [self.delegate splashAdDidClose];
    }
}

- (void)ksad_splashAd:(KSSplashAdView *)splashAdView didClick:(BOOL)inMiniWindow{
    if ([self.delegate respondsToSelector:@selector(splashAdDidClick)]) {
        [self.delegate splashAdDidClick];
    }
}

- (void)ksad_splashAd:(KSSplashAdView *)splashAdView didFailWithError:(NSError *)error{
    self.error = error;
    [self loadFlash];
}

@end
